package com.vir.ai.admin.utils;


import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

 
@Slf4j
public class AiFileUtil {
	private AiFileUtil() {
	}

	public static void streamToResponse(File destFile) throws Exception{
		log.info("正在写入response");
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		assert requestAttributes != null;
		HttpServletResponse response = requestAttributes.getResponse();

		response.setHeader("content-type", "application/octet-stream");
		response.setContentType("application/octet-stream");
		String fileName = java.net.URLEncoder.encode(destFile.getName(),"UTF-8").replace("+","%20");
		response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);

		byte[] buff = new byte[1024];
		try (OutputStream os = response.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(destFile))) {
			int i = bis.read(buff);
			while (i != -1) {
				os.write(buff, 0, i);
				os.flush();
				i = bis.read(buff);
			}
		}

		log.info("文件流写入完成");
	} 
}
